package com.teangtang.fly.repository;


import com.teangtang.fly.entity.Flight;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

public interface FlightRepository extends JpaRepository<Flight, String> {
    @Query("SELECT f FROM Flight f WHERE f.arrivalTime < :currentTime ORDER BY f.arrivalTime DESC")
    List<Flight> findFlightsBeforeCurrentTime(@Param("currentTime") Date currentTime);

    default Flight findClosestFlightByArrivalTime(Date currentTime) {
        List<Flight> Flights = findFlightsBeforeCurrentTime(currentTime);
        if (Flights.isEmpty()) {
            return null; // No flights found before current time
        } else {
            return Flights.get(0); // Return the flight with the closest arrival time
        }
    }

}
